//app.js
var util = require('./utils/util.js')
const blueTool = require('./utils/bluetool.js')

import touch from './utils/touch.js'//左滑右滑
import propsConfig from '/component/thorui/tui-config/index'
wx.$tui = propsConfig

App({
  touch: new touch(),
  onLaunch: function () {
    var that = this
    var token = wx.getStorageSync('token') || ''
    that.globalData.header.token = token
    // 登录获取js_code
    if (!that.globalData.js_code) {
      wx.login({
        success: function (res) {
          console.log("调用wx.login接口",res)
          if (res.code) {
            console.log('获取js_code成功！', that.globalData)
            that.globalData.js_code = res.code
          } else {
            console.log('获取js_code失败！' + res.errMsg)
          }
        }
      });
    }
    const baseVer = wx.getSystemInfoSync().SDKVersion
    blueTool.BleTool.getMiniSetting(baseVer)
  },
  isAuth:function(_callback) {
    var that = this;
    console.log('--首页换取token--');
    util.changeToken(that.globalData.header, _callback);
  },
  /**
   * 获取scroll-view的高度
   */
  getScrollHeight: function(fixedHeight){
    let height = fixedHeight?fixedHeight:0;
    let scrollHeight = this.globalData.windowHeight-height*this.globalData.prRate;
    return scrollHeight;
  },
  /**
   * 给globalData赋值
   */
  getWindowHeight: function(systemInfo){
    let that = this;
    let model = systemInfo.model.toLowerCase();
    let tabbarHeight = (model.indexOf('iphone 11')!=-1||model.indexOf('iphone x')!=-1)?85:45;

    that.globalData.systemInfo = systemInfo;
    that.globalData.windowHeight = systemInfo?systemInfo.windowHeight+tabbarHeight:'600';
    that.globalData.prRate = systemInfo.windowWidth/375;
  },
  //上报统计的数据
  sendLog: function(errUrl,errInfo,errData){
    var that = this;
    wx.request({
      url: util.CONST_DATA.BASE_URL + 'err/insertErrLog',
      method:'POST',
      header: that.globalData.header,
      data: {
        errUrl: errUrl,
        errInfo: errInfo,
        errData: errData,
      },
      complete: function (res) {
        console.log("----insertErrLog res=", res);
      }
    })


  },

  globalData: {
    phoneReg:/^1(3|4|5|6|7|8|9)\d{9}$/,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
      'token': '',
      'mini_ver':'1.2.1'
    },
    pass: wx.getStorageSync('pass'),
    reportId: '',
    userInfo: {},
    appid: 'wxf07c0b227b732f70',
    systemInfo: wx.getSystemInfoSync(),
    windowHeight:'600',
    prRate:1
  },
  getDeviceId: function(macId){
    if (macId) {
      return macId.substring(0, 2) + ':' + macId.substring(2, 4) + ':' + macId.substring(4, 6) + ':' + macId.substring(6, 8) + ':' + macId.substring(8, 10) + ':' + macId.substring(10, 12)
    } else {
      return '';
    }
  },
  executeRequest:function(callback) {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log('获取经纬度成功-->', res);
        that.globalData.header.lat = parseFloat(res.latitude)
        that.globalData.header.lng = parseFloat(res.longitude)
        if(callback) {
          callback()
        }
      },
      fail: function (res) {
        console.log('获取经纬度fail', res)
      }
    })
  }
})
